package com.zjdiepu.www.rest.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.ParentModel;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.mapper.yw.AbstractYwBusinessLoanVoNewMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.response.CalculateLoanResponse;

/**
 * 车贷计算
 * 项目名称：diepu-parent-rest <br/> 
 * 类名称：YwBusinessLoanController <br/>  
 * 类描述： 车贷计算<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年9月4日 上午10:54:02  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年9月4日 上午10:54:02   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value= "YwBusinessLoanController", produces="application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = {"车贷计算"}, produces="application/json;charset=UTF-8")
public class YwBusinessLoanController extends BaseController {
    @Resource
    private IYwBusinessLoanService ywBusinessLoanService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IYwBusinessCarService ywBusinessCarService;
    @Resource
    private AbstractYwBusinessLoanVoNewMapper ywBusinessLoanVoMapper;
    @Resource
    private TokenUserHelper tokenUserHelper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: calculate  <br/>
     * @Description: 车贷计算 <br/>
     * @param customerRestRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "车贷计算", response = CalculateLoanResponse.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("calculate")
    public ApiResponse calculate(@Validated(Add.class) @RequestBody YwBusinessLoan ywBusinessLoan) {
    	tokenUserHelper.getUser();
    	ywBusinessLoanService.calculation(ywBusinessLoan);
    	return ApiResponse.success(ywBusinessLoan);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getByBusiId  <br/>
     * @Description: 获取车贷信息 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取车贷信息", response = YwBusinessLoan.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getByBusiId")
    public ApiResponse getByBusiId(@Validated(Update.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	String busiId = parentModel.getId();
    	YwBusiness ywBusiness = ywBusinessService.selectById(busiId);
    	if(null == ywBusiness) {
    		return ApiResponse.fail("999", "订单数据丢失");
    	}
    	YwBusinessCar ywBusinessCar = new YwBusinessCar();
    	ywBusinessCar.setBusiId(busiId);
    	ywBusinessCar.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessCar = ywBusinessCarService.selectRefined(ywBusinessCar);
    	if(null == ywBusinessCar) {
    		return ApiResponse.fail("999", "请先保存车辆信息");
    	}
    	String carType = ywBusinessCar.getCarType();
    	if(StringUtils.isBlank(carType)) {
    		return ApiResponse.fail("999", "车辆性质没有填写");
    	}
    	
    	YwBusinessLoan ywBusinessLoan = new YwBusinessLoan();
    	ywBusinessLoan.setBusiId(busiId);
    	ywBusinessLoan.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessLoan = ywBusinessLoanService.selectRefined(ywBusinessLoan);
    	if(null == ywBusinessLoan) {
    		ywBusinessLoan = new YwBusinessLoan();
    	}
    	ywBusinessLoan.setLoanBank(ywBusiness.getBankLoan());
    	ywBusinessLoan.setCarPrice(ywBusinessCar.getPrice());
    	ywBusinessLoan.setCarType(carType);
    	
    	ywBusinessLoanVoMapper.mapper(ywBusinessLoan);
        return ApiResponse.success(ywBusinessLoan);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: saveLoan  <br/>
     * @Description: 保存车贷信息 <br/>
     * @param ywBusinessLoan
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保存车贷信息", response = String.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("saveLoan")
    public ApiResponse saveLoan(@Validated(Add.class) @RequestBody YwBusinessLoan ywBusinessLoan) {
    	SysUser user = tokenUserHelper.getUser();
    	String busiId = ywBusinessLoan.getBusiId();
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail("999", "业务ID不能为空");
    	}
    	YwBusinessCar ywBusinessCar = new YwBusinessCar();
    	ywBusinessCar.setBusiId(busiId);
    	ywBusinessCar.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessCar = ywBusinessCarService.selectRefined(ywBusinessCar);
    	if(null == ywBusinessCar) {
    		return ApiResponse.fail("999", "请先保存车辆信息");
    	}
    	ywBusinessLoan.setCarId(ywBusinessCar.getId());
    	
    	String carType = ywBusinessCar.getCarType();
    	if(StringUtils.isBlank(carType)) {
    		return ApiResponse.fail("999", "车辆性质没有填写");
    	}
    	ywBusinessLoan.setCarType(carType);
    	
    	ywBusinessLoanService.calculation(ywBusinessLoan);
    	ywBusinessLoan.setModifier(user.getId()); //统一使用修改人和修改人名称来初始化
    	ywBusinessLoan.setModifierName(user.getName());
    	
    	String id = ywBusinessLoan.getId();
    	if(StringUtils.isNotBlank(id)) {
    		ywBusinessLoanService.saveUpdateAndDelete(ywBusinessLoan, OperateType.edit);
    	} else {
    		ywBusinessLoanService.saveUpdateAndDelete(ywBusinessLoan, OperateType.add);
    	}
        return ApiResponse.success(ywBusinessLoan);
    }

}
